
import wx

import ab_ids as ai
import ab_statusbar


class DocumentFrame(wx.MDIChildFrame):
    def __init__(self, parent, app, handler, filename,
                 position=wx.DefaultPosition, size=(500, 500)):
        self.app = app
        self.handler = handler

        title = self.filename = filename

        cwp = app.readKey(ai.CfgParaCwd)
        plat = app.readKey(ai.CfgParaPlatform)
        filename = plat.getFileName(filename)
        start = filename.lower().startswith(cwp)

        if start:
            title = filename[len(cwp) + 1:]

        self.title = title
        self.parent = parent

        self.ret_val = False
        wx.MDIChildFrame.__init__(self, parent, -1, title)
        if not plat.hasStatusBar():
            self.statusbar = ab_statusbar.StatusBar(self.app, self)
        else:
            self.statusbar = None

        self.Show(False)
        wx.EVT_CLOSE(self, self.OnClose)
        wx.EVT_ACTIVATE(self, self.OnActivate)

    def OnActivate(self, event):
        self.app.warn('<DocumentFrame.OnActivate>: OnActivate not overrided')
        self.app.clearStatus()

        event.Skip()

    def OnClose(self, event):
        self.app.warn('<DocumentFrame.OnClose>: OnClose not overrided')
        if hasattr(self.app, 'closeDocument'):
            self.app.closeDocument(self.getTitle())

        self.app.clearStatus()
        event.Skip()

    def updateStatus(self, value, column):
        if self.statusbar is not None:
            self.statusbar.update(value, column)

    def getHandler(self):
        return self.handler

    def getTitle(self):
        return self.title

    def isReady(self):
        return self.ret_val

    def setReady(self):
        self.ret_val = True
        self.Show(True)


class DocumentHandler:
    def __init__(self, name, frame, singleton=False, update_menu=True):
        self.name = name
        self.frame = frame
        self.singleton = singleton
        self.update_menu = update_menu

    def getFrame(self, parent, app, *arg, **kws):
        return self.frame(parent, app, self, *arg, **kws)

    def getName(self):
        return self.name

    def isSingleton(self):
        return self.singleton

    def isUpdateToRecentMenu(self):
        return self.update_menu
